package com.hw.paltfrom.service.supervisor;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hw.paltfrom.pojo.HwBaseAnnexesfile;
import com.hw.paltfrom.pojo.HwBaseWorkflow;
import com.hw.paltfrom.service.doncutor.BaseWorkflowService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.paltfrom.exception.HWSystemException;
import com.hw.paltfrom.mapper.HwBaseAnnexesfileMapper;
import com.hw.paltfrom.mapper.HwSupervisionMapper;
import com.hw.paltfrom.pack.ResponseResult;
import com.hw.paltfrom.pojo.HwCommandWorkflow;
import com.hw.paltfrom.pojo.HwSupervision;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.service.doncutor.CommandWorkflowService;
import com.hw.paltfrom.utils.RandomNumberUtil;
import com.hw.paltfrom.utils.ResponeResultUtil;
import com.hw.paltfrom.vo.FileInfo;
import com.hw.paltfrom.vo.SupervisionVo;

@Service
public class SupervisoryBackServer {

	@Autowired
	private HwSupervisionMapper hwSupervisionMapper;
	
	@Autowired
	private CommandWorkflowService commandWorkflowService;

	@Autowired
	private BaseWorkflowService baseWorkflowService;
	// 附件表接口
	@Autowired
	private HwBaseAnnexesfileMapper hwBaseAnnexesfileMapper;

	public Map<String, Object> query(SupervisionVo vo) {

		// 分页
		Integer pageNum = vo.getPageNum();
		Integer pageSize = vo.getPageSize();
		PageHelper.startPage(pageNum, pageSize);

		// 督导内容
//		String fSpvcontent = vo.getfSpvcontent();
		//任务名称
		String fTitle = vo.getfTitle();
		// 办理阶段（待反馈-02；待完结-03；已完结-04）
		String fCurrentstep = vo.getfCurrentstep();
		// 系统登录人所属部门
		String fBackdepcode = vo.getfBackdepcode();
		// 模糊查询规则
		EntityWrapper<HwSupervision> wrapper = new EntityWrapper<HwSupervision>();

		if (StringUtils.isNotBlank(fCurrentstep)) {
			wrapper.eq("F_CurrentStep", fCurrentstep);
		} else {
			wrapper.ne("F_CurrentStep", "04");
		}
		
//		if (StringUtils.isNotBlank(fSpvcontent)) {
//			wrapper.like("F_SpvContent", fSpvcontent);
//		}
		
		if (StringUtils.isNotBlank(fTitle)) {
			wrapper.like("F_Title", fTitle);
		}

		// 指派给本县的或指派给本站（当前用户单位编码=反馈单位）
		wrapper.eq("F_BackDepCode", fBackdepcode).orderBy("F_FromTime", false);

		List<HwSupervision> hwSupervisionList = hwSupervisionMapper.selectList(wrapper);
		PageInfo<HwSupervision> info = new PageInfo<HwSupervision>(hwSupervisionList);

		return ResponseResult.successNotice(HWSystemCode.SUCCESS, info);
	}
	// 更新
	@Transactional(rollbackFor = { Exception.class, RuntimeException.class,HWSystemException.class })
	public Map<String, Object> feedback(SupervisionVo vo) {
		
		HwBaseWorkflow baseWorkflow = baseWorkflowService.getPresentWorkflow(vo.getfCurrentstep(), "JCDD");
		// 通过ID查询待更新的数据
		Wrapper<HwSupervision> wrapper = new EntityWrapper<HwSupervision>();
		wrapper.eq("F_SpvId", vo.getfSpvid());
		HwSupervision hwSupervision = new HwSupervision();
		// 记录是谁更新
		hwSupervision.setfBackuserid(vo.getfBackuserid());
		hwSupervision.setfBackusername(vo.getfBackusername());
		hwSupervision.setfBackuserphone(vo.getfBackuserphone());
		hwSupervision.setfBacktime(new Timestamp(System.currentTimeMillis()));
		hwSupervision.setfBackcontent(vo.getfBackcontent());
		hwSupervision.setfBackassignstate(vo.getfBackassignstate());
		hwSupervision.setIsStagestate(vo.getIsStagestate());
		hwSupervision.setfCurrentstep(baseWorkflow.getWorkflowNextcode());
//		vo.getFuJan()	
	     List<FileInfo> fileInfoList = vo.getFiLeInfo();
	   String number =  RandomNumberUtil.getNumber();
		int i =0;
		for (FileInfo fileInfo : fileInfoList) {
			++i;
			HwBaseAnnexesfile hwBaseAnnexesfile = new HwBaseAnnexesfile();
			hwBaseAnnexesfile.setFileId(RandomNumberUtil.getNumber());
			hwBaseAnnexesfile.setfFolderid(number);
			hwBaseAnnexesfile.setFileType(fileInfo.getFileType());
			hwBaseAnnexesfile.setFileName("反馈附件"+String.valueOf(i));
			hwBaseAnnexesfile.setFilePath(fileInfo.getFilePath());
			hwBaseAnnexesfile.setFileSize(fileInfo.getFileSize());
			hwBaseAnnexesfile.setFileMoudletype("JCCD检查督导");
			hwBaseAnnexesfile.setfCreateuserid(vo.getfBackuserid());
			hwBaseAnnexesfile.setfCreatedate(new Timestamp(System.currentTimeMillis()));
			hwBaseAnnexesfile.setfCreateusername(vo.getfFromusername());
			Integer insert = hwBaseAnnexesfileMapper.insert(hwBaseAnnexesfile);
			if (insert != 1) {
				return ResponeResultUtil.fail(HWSystemCode.FAIL);
			}
		}
		hwSupervision.setfBackfile(number);
		Integer update = hwSupervisionMapper.update(hwSupervision, wrapper);

		if (update != 1) {
			throw new HWSystemException();
		}
		//查询当前工作流
		// 审批流水数据
		HwCommandWorkflow hwCommandWorkflow = new HwCommandWorkflow();
		hwCommandWorkflow.setCommandworkflowId(RandomNumberUtil.getNumber());//主键
		hwCommandWorkflow.setWorkflowModule(baseWorkflow.getWorkflowModule());
		hwCommandWorkflow.setWorkflowType(baseWorkflow.getWorkflowType());
		hwCommandWorkflow.setWorkflowCode(baseWorkflow.getWorkflowCode());
		hwCommandWorkflow.setWorkflowCodename(baseWorkflow.getWorkflowCodename());
		hwCommandWorkflow.setCommandworkflowButtonname(vo.getButtonName());
		hwCommandWorkflow.setfAccount(vo.getfAccount());
		hwCommandWorkflow.setfFromdepcode(vo.getfUserDepCode());
		hwCommandWorkflow.setfFromdepname(vo.getGetfUserDepName());
		hwCommandWorkflow.setCommandworkflowDatatime(new Date());
		hwCommandWorkflow.setCommandworkflowContent(vo.getfBackcontent());
		hwCommandWorkflow.setObjectId(vo.getfSpvid());
		hwCommandWorkflow.setWorkflowNextcode(baseWorkflow.getWorkflowNextcode());
		hwCommandWorkflow.setWorkflowNextcodename(baseWorkflow.getWorkflowNextcodename());
		hwCommandWorkflow.setfAnnexes(number);
		

		// 向各阶段审批操作过程表插入数据
		boolean success = commandWorkflowService.addWorkflow(hwCommandWorkflow);
		if (!success) {
			throw new HWSystemException();
		}

		return ResponeResultUtil.success(HWSystemCode.SUCCESS);
	}
	
	// 详情
		public Map<String, Object> queryDetails(String fSpvid) {
			HashMap<String, Object> result = new HashMap<String, Object>();
			// 督导表
			EntityWrapper<HwSupervision> supervision = new EntityWrapper<HwSupervision>();
			supervision.eq("F_SpvId", fSpvid);
			List<HwSupervision> hwSupervisions = hwSupervisionMapper.selectList(supervision);
			List<SupervisionVo> supervisionVo = new ArrayList<>();
			for (HwSupervision hwSupervision1 : hwSupervisions) {
				SupervisionVo supervisionVo1 = new SupervisionVo();
				supervisionVo1.setfSpvid(hwSupervision1.getfSpvid());
				supervisionVo1.setfFromobjectid(hwSupervision1.getfFromobjectid());
				supervisionVo1.setfFromtype(hwSupervision1.getfFromtype());
				supervisionVo1.setfFromdepcode(hwSupervision1.getfFromdepcode());
				supervisionVo1.setfFromuserid(hwSupervision1.getfFromuserid());
				supervisionVo1.setfFromusername(hwSupervision1.getfFromusername());
				supervisionVo1.setfFromtime(hwSupervision1.getfFromtime());
				supervisionVo1.setfTitle(hwSupervision1.getfTitle());
				supervisionVo1.setfFromsrc(hwSupervision1.getfFromsrc());
				supervisionVo1.setfSpvcontent(hwSupervision1.getfSpvcontent());
				supervisionVo1.setfCurrentstep(hwSupervision1.getfCurrentstep());
				supervisionVo1.setfBacktype(hwSupervision1.getfBacktype());
				supervisionVo1.setfBackdepcode(hwSupervision1.getfBackdepcode());
				supervisionVo1.setfBackdepname(hwSupervision1.getfBackdepname());
				supervisionVo1.setfBackuserid(hwSupervision1.getfBackuserid());
				supervisionVo1.setfBackusername(hwSupervision1.getfBackusername());
				supervisionVo1.setfBackuserphone(hwSupervision1.getfBackuserphone());
				supervisionVo1.setfBackcontent(hwSupervision1.getfBackcontent());
				supervisionVo1.setfBacktime(hwSupervision1.getfBacktime());
				supervisionVo1.setfBackfile(hwSupervision1.getfBackfile());
				supervisionVo1.setfBackassignstate(hwSupervision1.getfBackassignstate());
				supervisionVo1.setIsStagestate(hwSupervision1.getIsStagestate());

				// 督导附件数据
			EntityWrapper<HwBaseAnnexesfile> annexesFile = new EntityWrapper<HwBaseAnnexesfile>();
				annexesFile.eq("F_FolderId", hwSupervision1.getfBackfile());
				List<HwBaseAnnexesfile> list = hwBaseAnnexesfileMapper.selectList(annexesFile);
				supervisionVo1.setFuJan(list);
				supervisionVo.add(supervisionVo1);
			}
			// 督导表
			result.put("checkTheSupervision", supervisionVo);
		
//		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, result);
		return result;
		}
}
